﻿//  Copyright (c) 2009 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System.Linq;
using System.Web.Mvc;
using DNA.Mvc.Community;
using DNA.Mvc.jQuery;

namespace DNA.Mvc.Community.Controllers
{
    public class ThreadController : Controller
    {
        private IForumContext forumContext;

        public ThreadController(IForumContext dbContext)
        {
            forumContext = dbContext;
        }

        [Pagable]
        public ActionResult Index(int id, QueryParams _params)
        {
            var thread = forumContext.Threads.Find(id);
            if (thread == null)
                return HttpNotFound();

            if (Request.QueryString.AllKeys.Contains("index"))
            {
                var _index = Request.QueryString["index"];
                if (string.IsNullOrEmpty(_index))
                    return RedirectToAction("Index", new { id = id });
            }

            if ((!thread.Forum.AllowAnonymous) && (!Request.IsAuthenticated))
                return RedirectToAction("Login", "Account", new { Area = "", returnUrl = Url.Action("Threads", "Forum", ControllerContext.RouteData.Values) });

            int total = thread.Posts.Count;
            int skip = _params.GetSkipRecordCount(true);
            var model = skip > 0 ? thread.Posts.OrderBy(p => p.Pos).Skip(skip).Take(_params.Size).ToList() : thread.Posts.OrderBy(p => p.Pos).Take(_params.Size);

            //ViewBag.IsSubscribed =Request.IsAuthenticated ? notify.IsSubscribed(thread.SubscriptionToken, User.Identity.Name) : false;
            ViewBag.Thread = new ThreadWrapper(thread);
            ViewBag.NextThread = forumContext.Threads.Next(thread.ForumID, id);
            ViewBag.PrevThread = forumContext.Threads.Prev(thread.ForumID, id);
            ViewBag.Forum = new ForumWrapper(thread.Forum, forumContext);
            //ViewBag.Ranks = forumContext.Ranks.All().ToList();

            WebSiteContext.Current.Title = thread.Title;
            if (!string.IsNullOrEmpty(thread.ThreadPost.Body))
            {
                var summary = Text.TextEngine.Text(Text.TextEngine.BBCode(thread.ThreadPost.Body).ToHtmlString());
                if (summary.Length > 160)
                    summary = summary.Substring(0, 160);
                WebSiteContext.Current.Description = summary;
            }

            if (_params.Index == 1)
            {
                if (Request.IsAuthenticated)
                {
                    if (!thread.ThreadPost.UserName.Equals(User.Identity.Name))
                    {
                        forumContext.Threads.Read(id);
                        forumContext.SaveChanges();
                    }
                }
                else
                {
                    forumContext.Threads.Read(id);
                    forumContext.SaveChanges();
                }
            }
            return View(new ModelWrapper(model, total));
        }

        [Authorize]
        public ActionResult ModeratedThreads(int id, int? index, int? size)
        {
            int pageIndex = index == null ? 0 : index.Value;
            int pageSize = size == null ? 20 : size.Value;
            int total = 0;
            ViewData.Model = forumContext.Threads.GetModeratedThreads(id, pageIndex, pageSize, out total).ToList();
            ViewData["PageIndex"] = pageIndex;
            ViewData["PageSize"] = pageSize;
            ViewData["TotalRecords"] = total;
            return PartialView();
        }
    }
}
